package com.holystone.splunk.common;

import com.github.pagehelper.PageInfo;
import org.springframework.ui.ModelMap;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PageData implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<?> pageList;
	private Long totalCount;
	private Integer pageSize;
	private Integer pageNo;
	private Integer totalPage;

	/**
	 *fengtao
	 * 2016-10-19
	 * 页面中title显示内容
	 */
	private Map title;


	public List<?> getPageList() {
		return pageList;
	}

	public void setPageList(List<?> pageList) {
		this.pageList = pageList;
	}

	public Long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Long totalCount) {
		this.totalCount = totalCount;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	public Map getTitle() {
		return title;
	}

	public void setTitle(Map title) {
		this.title = title;
	}


	public static PageData getPageData(Integer no, Integer size, List<?> list) {
        PageData page = new PageData();
        Integer start = (no - 1) * size;
        Integer end = no * size;
        Integer total = list.size() % size == 0 ? list.size() / size : list.size() / size + 1;
        if (list != null && list.size() <= end) {
            page.pageList = list.subList(start, list.size());
        } else if (list != null && list.size() > end) {
            page.pageList = list.subList(start, end);
        }
        page.pageNo = no;
        page.pageSize = size;
        page.totalCount = Long.valueOf(list.size());
        page.totalPage = total;
        return page;
    }

	public static ModelMap getReturnMap(PageInfo<?> pageInfo) {
		PageData pageData = new PageData();
		if (pageInfo != null){
            pageData.setPageList(pageInfo.getList());
            pageData.setTotalCount(pageInfo.getTotal());
            pageData.setPageSize(pageInfo.getPageSize());
            pageData.setPageNo(pageInfo.getPageNum());
            pageData.setTotalPage(pageInfo.getPages());
            if (pageData.getPageList() == null || pageData.getPageList().size() == 0) {
                return JSONMapper.createReturnMap(SystemConstant.RESPONSE_CODE_SUCCESS_NULL_COMTENT,null, null, null);
            }else{
                return JSONMapper.createReturnMap(SystemConstant.RESPONSE_CODE_SUCCESS,null, pageData, null);
            }
        }
        return JSONMapper.createReturnMap(SystemConstant.RESPONSE_CODE_RESOURCE_NOT_EXISTS,null, null, null);
	}
	
	public static PageData getReturnPageData(PageInfo<?> pageInfo) {
		PageData pageData = new PageData();
		pageData.setPageList(pageInfo.getList());
		pageData.setTotalCount(pageInfo.getTotal());
		pageData.setPageSize(pageInfo.getPageSize());
		pageData.setPageNo(pageInfo.getPageNum());
		pageData.setTotalPage(pageInfo.getPages());
		return 	pageData;
	}
	
	public static ModelMap getReturnMap(List<?> list) {
		PageData pageData = new PageData();
		pageData.setPageList(list);
		if (pageData.getPageList().size() == 0) {
			return JSONMapper.createReturnMap(SystemConstant.RESPONSE_CODE_SUCCESS_NULL_COMTENT,null, null, null);
		}else{
			return JSONMapper.createReturnMap(SystemConstant.RESPONSE_CODE_SUCCESS,null, pageData, null);
		}	
	}
}
